package com.jader.activities;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jader.adapter.MyTendsAdapter;
import com.jader.beans.Response;
import com.jader.myviews.CustomListView;
import com.jader.myviews.MyProgressDialog;
import com.jader.myviews.CustomListView.OnLoadMoreListener;
import com.jader.myviews.CustomListView.OnRefreshListener;
import com.jader.myviews.DeleteShopcartAlertDialog;
import com.jader.util.JaderApplication;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.zlp.jader.R;

public class MyTendsActivity extends Activity {
	private List<Response> list = new ArrayList<Response>();
	private TextView wode;
	private ImageView back;
	private MyTendsAdapter myTendsAdapter;
	private CustomListView listView;
	LayoutInflater mInflater;
	int userid;
	int wodedongtai = 0;
	String url = JaderApplication.getIP() + "/Jader/SelectAll?type=35&userid=";
	private String url1 = JaderApplication.getIP() + "/Jader/client";
	private MyProgressDialog dialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		userid = JaderApplication.user.getUserid();
		dialog = JaderApplication.getMyDialog(this);
		setContentView(R.layout.activity_my_tends);
		wode = (TextView) findViewById(R.id.top_actiobar_text);
		wode.setText("我的动态");
		wode.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	@Override
	protected void onResume() {
		liebiao();
		super.onResume();
	}

	private void liebiao() {
		// 初始化尾部视图
		mInflater = LayoutInflater.from(MyTendsActivity.this);
		listView = (CustomListView) findViewById(R.id.MyTends_ListView);
		back = (ImageView) findViewById(R.id.top_actionbar_back);
		// 创建自定义适配器实例
		myTendsAdapter = new MyTendsAdapter(list, MyTendsActivity.this);
		listView.setAdapter(myTendsAdapter);
		dialog.show();
		http(1);
		listView.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh() {
				wodedongtai = 0;
				http(0);
				// TODO 下拉刷新
				// loadData(0);
			}
		});

		listView.setOnLoadListener(new OnLoadMoreListener() {

			@Override
			public void onLoadMore() {
				// TODO 加载更多
				wodedongtai = list.size();
				http(1);
				// loadData(1);

			}
		});
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Response response = list.get(position - 1);
				if (response.getIsRead() == 0) {
					readMessage(response);
				}
				Intent intent = new Intent(MyTendsActivity.this,
						SuoYouDetail.class);
				intent.putExtra("tiezi_id", list.get(position - 1)
						.getPrente_id());
				startActivity(intent);
			}
		});
		listView.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {
				DeleteShopcartAlertDialog deleteShopcartAlertDialog = new DeleteShopcartAlertDialog(
						MyTendsActivity.this).builder();
				deleteShopcartAlertDialog.setTitle("確定要刪除这条消息吗？");
				deleteShopcartAlertDialog.setNegativeButton("取消", null);
				final int xiaoxiid = position - 1;

				deleteShopcartAlertDialog.setPositiveButton("确认",
						new OnClickListener() {

							@Override
							public void onClick(View arg0) {
								deleteMessage(xiaoxiid);

							}
						});
				deleteShopcartAlertDialog.show();
				return true;
			}
		});
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
	}

	private void deleteMessage(final int xiaoxiid) {
		dialog.show();
		HttpUtils httpUtils = new HttpUtils();
		RequestParams requestParams = new RequestParams();
		requestParams.addBodyParameter("type", 36 + "");
		requestParams.addBodyParameter("xiaoxi_id", list.get(xiaoxiid)
				.getXiaoxiid() + "");
		httpUtils.send(HttpMethod.POST, url1, requestParams,
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO Auto-generated method stub
						if (dialog.isShowing()) {
							dialog.dismiss();
						}
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						if ("true".equals(arg0.result)) {
							list.remove(xiaoxiid);
							myTendsAdapter.notifyDataSetChanged();
							JaderApplication.show(MyTendsActivity.this, "删除成功");
						}
						if (dialog.isShowing()) {
							dialog.dismiss();
						}
					}
				});
	}

	// 监听列表单击事件
	//
	private void readMessage(final Response response) {
		RequestParams params = new RequestParams();
		params.addBodyParameter("type", JaderApplication.UPDATE_XIAOXI_YIDU
				+ "");
		params.addBodyParameter("xiaoxiid", response.getXiaoxiid() + "");
		String url = JaderApplication.getIP() + "/Jader/client";
		JaderApplication.httpUtils.send(HttpMethod.POST, url, params,
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						if ("ture".equals(arg0.result)) {
							response.setIsRead(1);
						}

					}
				});
	}

	private void http(final int type) {
		HttpUtils httpUtils = new HttpUtils();
		String newurl = url + userid + "&wodedongtai=" + wodedongtai;
		httpUtils.send(HttpMethod.GET, newurl, new RequestCallBack<String>() {

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				// TODO Auto-generated method stub
				Toast.makeText(MyTendsActivity.this, "没有动态", Toast.LENGTH_SHORT)
						.show();
				if (dialog.isShowing()) {
					dialog.dismiss();
				}
			}

			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				// TODO Auto-generated method stub
				Gson gson = new Gson();
				Type typelist = new TypeToken<List<Response>>() {
				}.getType();
				List<Response> liebiaos = gson.fromJson(arg0.result, typelist);
				if (type == 0) {
					// 解决重复数据
					int result = 0;
					for (Response suoYou_Detail : liebiaos) {
						boolean flag = true;
						for (Response suoYou_Detail2 : list) {
							if (suoYou_Detail.getTiezi_id() == suoYou_Detail2
									.getTiezi_id()) {
								flag = false;
							}
						}
						if (flag) {
							list.add(result, suoYou_Detail);
							result++;
						}
					}
					myTendsAdapter.notifyDataSetChanged();

				} else if (type == 1) {
					for (Response suoYou_Detail : liebiaos) {

						boolean flag = true;
						for (Response suoYou_Detail2 : list) {
							if (suoYou_Detail.equals(suoYou_Detail2)) {
								flag = false;
							}
						}
						if (flag) {
							list.add(suoYou_Detail);
						}
					}
					myTendsAdapter.notifyDataSetChanged();
				}
				listView.onRefreshComplete();
				listView.onLoadMoreComplete();
				if (dialog.isShowing()) {
					dialog.dismiss();
				}
			}
		});
	}
}
